<template>
  <div class="meal_management">
    <div class="header_title">
      <span>餐别设置列表</span>
    </div>
    <div class="meal_table">
      <el-table
        :data="data1"
        highlight-current-row
        style="width: 100%"
        :header-cell-style="headFirst"
        :cell-style="(res) => headFirst_col(res, [], 2)"
        border
      >
        <el-table-column prop="mealName" label="餐别"></el-table-column>
        <el-table-column label="起始时间">
          <template slot-scope="scope">
            <el-time-picker
              arrow-control
              v-model="scope.row.startTime"
              value-format="HH:mm:ss"
              size="mini"
              :picker-options="{
                start: '00:00',
                step: '00:01',
                end: '23:59'
              }"
              disabled
            ></el-time-picker>
          </template>
        </el-table-column>
        <el-table-column prop="endTime" label="终止时间">
          <template slot-scope="scope">
            <el-time-picker
              arrow-control
              v-model="scope.row.endTime"
              value-format="HH:mm:ss"
              size="mini"
              :picker-options="{
                start: '00:00',
                step: '00:01',
                end: '23:59'
              }"
              @change="(res) =>handleChange(res,scope)"
              :disabled="scope.$index == 3"
            ></el-time-picker>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              title="修改"
              type="primary"
              size="mini"
              @click="editUserForm(scope.row)"
              icon="el-icon-edit"
            ></el-button>
          </template>
        </el-table-column>-->
      </el-table>
      <div class="button">
        <el-button type="primary" @click="FormSubmit()" v-if="isauth('meal_edit')" size="mini">保存修改</el-button>
      </div>
    </div>
    <!-- 餐别设置弹窗 -->
    <el-dialog
      width="500px"
      custom-class="customWidth"
      center
      :before-close="closeDialog"
      :title="titlename"
      :close-on-click-modal="false"
      :visible.sync="eidtDialogFormVisible"
    >
      <el-form :model="eidtdataForm" :rules="eidtdtataRule" ref="eidtdataForm" label-width="100px">
        <el-form-item label="起始时间" prop="parentMerchantGuid">
          <el-col :span="15">
            <el-time-picker
              arrow-control
              v-model="eidtdataForm.startTime"
              value-format="HH:mm:ss"
              size="mini"
              :picker-options="{
                start: '00:00',
                step: '00:01',
                end: '23:59'
              }"
            ></el-time-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="终止时间">
          <el-col :span="15">
            <el-time-picker
              arrow-control
              v-model="eidtdataForm.endTime"
              value-format="HH:mm:ss"
              size="mini"
              :picker-options="{
                start: '00:00',
                step: '00:01',
                end: '23:59',
                minTime: eidtdataForm.startTime
              }"
            ></el-time-picker>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="eidtvisible()">取消</el-button>
        <el-button type="primary" @click="eidtdataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data1: [],
      dataTime:[],
      eidtDialogFormVisible: false,
      eidtdataForm: {
        startTime: "",
        endTime: ""
      },
      titlename: "",
      eidtdtataRule: {}
    };
  },
  mounted() {
    this.mealtypelist();
  },
  computed: {
    changeschoolid: {
      get() {
        return this.$store.state.user.changeschoolid;
      }
    }
  },
  methods: {
    // 结束时间选择
    handleChange(res,scope){
      let endTime = this.dataTime[scope.$index].endTime;
      if(scope.row.startTime >= res){
         this.$message({
          message: "起始时间不能大于终止时间",
          type: "warning",
        });
        scope.row.endTime = endTime;
        return ;
      }
      if(this.data1[scope.$index + 1].endTime <= res){
         this.$message({
          message: `${scope.row.mealName}终止时间不能大于${this.data1[scope.$index + 1].mealName}的终止时间`,
          type: "warning",
        });
        scope.row.endTime = endTime;
        return ;
      }
      this.data1[scope.$index + 1].startTime = res;
    },
    //餐别资料查询
    mealtypelist() {
      this.$http({
        url: this.$http.adornUrl("/unit/unit-meal-type-list"),
        method: "post",
        data: this.$http.adornData({
          messageContent: this.changeschoolid,
          messageType: 0
        })
      }).then(({ data }) => {
        if (data.messageCode == 200) {
          this.data1 = data.messageContent;
          this.dataTime = JSON.parse(JSON.stringify(data.messageContent));
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    },
    editUserForm(row) {
      console.log(row);
      this.titlename = row.mealName;
      this.eidtdataForm.startTime = row.startTime;
      this.eidtdataForm.endTime = row.endTime;
      this.eidtDialogFormVisible = true;
    },
    eidtvisible() {
      this.$refs["eidtdataForm"].resetFields();
      this.eidtDialogFormVisible = false;
    },
    eidtdataFormSubmit() {
      console.log(this.startTime);
      console.log(this.endTime);
    },
    closeDialog(done) {
      this.$refs["eidtdataForm"].resetFields();
      done();
    },
    FormSubmit() {
      this.$http({
        url: this.$http.adornUrl("/unit/unit-meal-type-update"),
        method: "post",
        data: this.$http.adornData({
          messageContent: this.data1,
          messageType: 0
        })
      }).then(({ data }) => {
        console.log(data);
        if (data.messageCode == 200) {
          this.$message({
            type: "success",
            message: "保存修改成功!"
          });
          this.mealtypelist();
        } else {
          this.$message.error(data.errorMessage);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.meal_management {
  padding: 10px;
  .header_title {
    // text-indent: 22px;
    height: 50px;
    line-height: 50px;
    border-bottom: 1px solid #e5e9f2;
    color: #909399;
    margin-bottom: 10px;
  }
  .button {
    padding-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>
